/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReciteTest_Spell.h"

ReciteTest_Spell::ReciteTest_Spell(WordBrowser* display, QWidget* parent):
	ReciteTestMethodBase(display, parent),
	submitMySpellButton(NULL),
	submitMySpellButtonShortCut(NULL),
	spellHintDisplay(NULL),
	isTesting(false)
{
	submitMySpellButton = new QPushButton("Submit", this);
	testMethodGrid->addWidget(submitMySpellButton,1,1);
	submitMySpellButton->hide();

	spellHintDisplay = new QTextBrowser(this);
	testMethodGrid->addWidget(spellHintDisplay, 2, 1);
	spellHintDisplay->hide();

	submitMySpellButtonShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return),  this);
	//submitMySpellButtonShortCut = new QShortcut(QKeySequence(Qt::Key_Return),  this);
	submitMySpellButtonShortCut->setEnabled(false);
	QObject::connect(submitMySpellButton,SIGNAL(clicked()),this,SLOT(showSpellTestAnswer()));
	QObject::connect(submitMySpellButtonShortCut,SIGNAL(activated()),submitMySpellButton,SIGNAL(clicked()));
	QObject::connect(textDisplay->getTextBrowser(),SIGNAL(textChanged()),this,SLOT(spellTextChanged()));
}

ReciteTest_Spell::~ReciteTest_Spell()
{
}

void ReciteTest_Spell::beginTest()
{
	textDisplay->getTextBrowser()->clear();
	ReciteTestMethodBase::beginTest();
	textDisplay->getTextBrowser()->setReadOnly(false);
	textDisplay->getTextBrowser()->setUndoRedoEnabled(true);
	showSpellTestPrompeExp();
	textDisplay->getTextBrowser()->insertHtml(displayHtmlString);
	textDisplay->getTextBrowser()->moveCursor(QTextCursor::Start);
	showSpellTestButtons();
	isTesting = true;
	DM.lookup(WM.getCurWord());
}

void ReciteTest_Spell::endTest()
{
	ReciteTestMethodBase::endTest();
}

void ReciteTest_Spell::resetDynamicButtons()
{
	ReciteTestMethodBase::resetDynamicButtons();
	spellHintDisplay->hide();
	submitMySpellButton->hide();
	submitMySpellButtonShortCut->setEnabled(false);
}

void ReciteTest_Spell::showNextButton()
{
	resetDynamicButtons();
	ReciteTestMethodBase::showNextButton();
}

void ReciteTest_Spell::showSpellTestButtons()
{
	resetDynamicButtons();
	spellHintDisplay->show();
	submitMySpellButton->show();
	submitMySpellButtonShortCut->setEnabled(true);

	QString English = WM.getCurWord()->getEnglish();
	int totalCharCount = English.size();
	spellHintDisplay->clear();
	spellHintDisplay->insertHtml(QString("Undo: Ctrl+z.<br>"));
	spellHintDisplay->insertHtml(QString("Redo: Ctrl+Shift+z.<br>"));
	spellHintDisplay->insertHtml(QString("Submit: Ctrl+Enter.<br>"));
	spellHintDisplay->insertHtml(QString("Total char count: %1.<br>").arg(totalCharCount));
	//spellHintDisplay->insertHtml(QString("Start with '%1'.<br>").arg(English[0]));
	submitMySpellButtonShortCut->setEnabled(true);
}

void ReciteTest_Spell::spellTextChanged()
{
	if (isTesting)
	{
		QString curAnswer = textDisplay->getTextBrowser()->toPlainText();
		int answerEndPos = curAnswer.indexOf("\n");
		curAnswer = curAnswer.left(answerEndPos);
		curAnswer = curAnswer.simplified();
		int curAnswerSize = curAnswer.size();
		QString previousText = spellHintDisplay->toHtml();
		int validPos = previousText.lastIndexOf("Current char count:");
		previousText = previousText.left(validPos);
		spellHintDisplay->clear();
		spellHintDisplay->insertHtml(previousText);
		spellHintDisplay->insertHtml(QString("Current char count: '%1'.").arg(curAnswerSize));
	}
}

void ReciteTest_Spell::showSpellTestPrompeExp()
{
	//textDisplay->insertHtml("<br>");
	Word* word = WM.getCurWord();
	if(word)
	{
		QString English = word->getEnglish();
		displayHtmlString.append(QString("<br>"));
		displayHtmlString.append(English[0]);
		for (int i=0; i<English.size()-2; i++ )
			displayHtmlString.append("_");
		displayHtmlString.append(English[English.size()-1]);
		displayHtmlString.append(QString("<br>") + word->getRedBibleExp() + QString("<br>"));
	}
		else
	{
		cout<<"ReciteTest_Spell::showSpellTestPrompeExp-->Cannot get word. "<<endl;
		QCoreApplication::quit();
	}

	//if (OPT.SpellTestHintDict.contains(DictNames::XDICTDict))
	//	insertCensoredXDICTExp();

	//if (OPT.SpellTestHintDict.contains(DictNames::LongmanDict))
	//	insertCensoredLongmanExp();

	float newLevel=WM.getCurWord()->getMemoryLevel();
	emit memoryLevelChangedTo(int(100*newLevel));
}


void ReciteTest_Spell::showSpellTestAnswer()
{
	isTesting = false;
	textDisplay->getTextBrowser()->setReadOnly(true);
	textDisplay->getTextBrowser()->setUndoRedoEnabled(false);

	QString answer = textDisplay->getTextBrowser()->toPlainText();
	int answerEndPos = answer.indexOf("\n");
	answer = answer.left(answerEndPos);
	answer = answer.simplified();

	textDisplay->getTextBrowser()->clear();
	textDisplay->showEnglish();
	textDisplay->showSoundMark();
	textDisplay->getTextBrowser()->insertHtml("<br>****************************************************<br>");

	if(answer == WM.getCurWord()->getEnglish())
	{
		textDisplay->getTextBrowser()->insertHtml(QString("You answer is <FONT COLOR='green'>CORRECT</FONT>."));
		spellHintDisplay->setText("You spell correctly.");
		remember();
	}
	else
	{
		textDisplay->getTextBrowser()->insertHtml(QString("You answer \"%1\" is <FONT COLOR='red'>WRONG</FONT>.").arg(answer));
		spellHintDisplay->setText("You spell wrong.");
		forget();
	}
	textDisplay->getTextBrowser()->insertHtml("<br>****************************************************<br>");
	textDisplay->showAllExplanations();
}

